shadow:utility@Map<K,V>
class HashMap<K is CanHash and CanEqual<K>, V is CanEqual<V>>
Class HashMap<K,V>
stores a map, also known as a symbol table, of key-value pairs with entries mapping keys of type K
to values of type V
. This map is implemented with a hash table that employs the chaining strategy for collision resolution, allowing keys to be added, found, and deleted in constant or near-constant time. HashMap<K,V>
requires that type K
has the CanHash
interface but imposes no ordering requirement on keys. The TreeMap<K,V>
class should be considered if key ordering is a requirement.
Modifiers | Return Types | Method and Description |
---|---|---|
public |
() |
create() Creates an empty |
public |
() |
create(long initialCapacity) Creates an empty |
public |
() |
create(long initialCapacity, float loadFactor) Creates a |
Modifiers | Return Types | Method and Description |
---|---|---|
public |
() |
destroy() |
Modifiers | Return Types | Method and Description |
---|---|---|
public |
(nullable V) |
add(K key, V value) Stores value object in the location associated with the key and returns the old value if there was one at that location. |
public |
(HashMap<K,V>) |
clear() Removes all entries from the map and resets the capacity to default. |
public readonly |
(boolean) |
containsKey(K key) Checks to see if the map contains a key. |
public readonly |
(boolean) |
containsValue(V value) Checks to see if the map contains a particular value. |
public readonly |
(HashMap<K,V>) |
copy(AddressMap addresses) |
public readonly |
(nullable V) |
index(K key) Retrieves the value associated with the key. |
public |
() |
index(K key, V value) Stores value object in the location associated with the key. |
public readonly |
(boolean) |
isEmpty() Checks whether or not the map is empty. |
public readonly |
(Iterator<V>) |
iterator() Creates an iterator to iterate over all the values in the map. |
public |
(nullable V) |
remove(K key) Removes the key-value pair associated with the key location. |
protected |
() |
resize(long newCapacity) |
public readonly |
(String) |
toString() Produces a |
Modifiers | Return Types | Method and Description |
---|---|---|
public readonly get |
(int) |
size() |
public readonly get locked |
(long) |
sizeLong() Gets the number of key-value pairs in the map. |
public create() => ()
Creates an empty HashMap
with a default capacity of 16 and maximum load factor of 0.75.
public create(long initialCapacity) => ()
Creates an empty HashMap
with the specified capacity and maximum load factor.
initialCapacity
- initial capacity of the map
loadFactor
- maximum load factor before the map is resized
public create(long initialCapacity, float loadFactor) => ()
Creates a HashMap
with the specified initial capacity of buckets and the specified maximum load factor before all the key-value pairs are re-hashed. Note that the capacity will always be increased to the next power of 2 if it is not a power of 2.
initialCapacity
- initial number of buckets in the hash table
loadFactor
- maximum load factor (buckets / keys) before all key-value pairs are re-hashed
public destroy() => ()
public add(K key, V value) => (nullable V)
Stores value object in the location associated with the key and returns the old value if there was one at that location. This operation runs in constant time in the best case and linear (but amortized constant) time if adding triggers a table resize.
key
- key location
value
- value to store
old value at location or null
if key was not already present
public clear() => (HashMap<K,V>)
Removes all entries from the map and resets the capacity to default.
map after being cleared
public readonly containsKey(K key) => (boolean)
Checks to see if the map contains a key. This operation runs in constant time or near-constant time.
key
- key to find
true
if present
public readonly containsValue(V value) => (boolean)
Checks to see if the map contains a particular value. This operation runs in time linear in the capacity of the hash table.
value
- value to find
true
if present
public readonly copy(AddressMap addresses) => (HashMap<K,V>)
public readonly index(K key) => (nullable V)
Retrieves the value associated with the key. If the key is not present, null
is returned. This operation runs in constant or near-constant time.
key
- key to find
value at key location or null
if not found
public index(K key, V value) => ()
Stores value object in the location associated with the key. This operation runs in constant time in the best case and linear (but amortized constant) time if adding triggers a table resize.
key
- key location
value
- value to store
public readonly isEmpty() => (boolean)
Checks whether or not the map is empty.
true
if the map is empty
public readonly iterator() => (Iterator<V>)
Creates an iterator to iterate over all the values in the map.
iterator
public remove(K key) => (nullable V)
Removes the key-value pair associated with the key location. This operation runs in constant time or near-constant time.
key
- key to remove
value being removed or null
if not present
protected resize(long newCapacity) => ()
public readonly toString() => (String)
Produces a String
representation of the map, listing all key-value pairs in an unspecified order.
String
representation